//
//  MeViewController.m
//  CarLifes
//
//  Created by yu on 2017/6/14.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "MeViewController.h"
#import "MeHeaderView.h"
#import "MeViewCell.h"
#import "HFStretchableTableHeaderView.h"
#import "UserInfoSettingViewController.h"
#import "CLSettingViewController.h"
#import "UIImage+CLImage.h"
#import "UIImageView+LBBlurredImage.h"
#import "AboutUsViewController.h"
#import "Feedback1ViewController.h"
#import "TabBarController.h"
#import "UIView+TransitionAnimation.h"
#import "ModifyPsdViewController.h"

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

#define kMeTableViewCellId @"MeTableViewCell"

#define DefaultLocationTimeout 2
#define DefaultReGeocodeTimeout 2

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,AMapSearchDelegate,UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) MeHeaderView *headerView;

@property (nonatomic, strong) HFStretchableTableHeaderView *stretchHeaderView;

@property (nonatomic, strong) AMapSearchAPI *search;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.fd_prefersNavigationBarHidden = YES;
    
    [self setupHeaderView];
    
    [self setRightNormalImageNamed:@"setting" selectImageNamed:nil action:@selector(settingAction)];
    
    [self.tableView registerClass:[MeViewCell class] forCellReuseIdentifier:kMeTableViewCellId];
    
    [self configSearchAPI];
    
    [self searchLiveWeather:self.user.city];
    
}

#pragma mark - Utility -

- (void)configSearchAPI
{
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
}

- (void)searchLiveWeather:(NSString *)city
{
    AMapWeatherSearchRequest *request = [[AMapWeatherSearchRequest alloc] init];
    request.city = city;
    request.type = AMapWeatherTypeLive;
    
    [self.search AMapWeatherSearch:request];
}

#pragma mark - AMapSearchDelegate

- (void)onWeatherSearchDone:(AMapWeatherSearchRequest *)request response:(AMapWeatherSearchResponse *)response
{
    if (request.type == AMapWeatherTypeLive)
    {
        if (response.lives.count == 0)
        {
            return;
        }
        
        AMapLocalWeatherLive *liveWeather = [response.lives firstObject];
        if (liveWeather != nil)
        {
            [self.headerView.headerSubview updateWeatherWithInfo:liveWeather];
        }
    }
}

- (void)setupHeaderView
{
    self.headerView = [[MeHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 250)];
    self.headerView.headerSubview.nameLabel.text = self.user.deptName;
//    [self.headerView.backgroundImageView setImageToBlur:IMAGE_NAMED(@"picture01") blurRadius:20 completionBlock:nil];
    [self.stretchHeaderView stretchHeaderForTableView:self.tableView withView:self.headerView.backgroundImageView subViews:self.headerView.headerSubview];
    
    @weakify(self);
    self.headerView.headerSubview.iconBlock = ^{
        @strongify(self);
        UserInfoSettingViewController *setting = [[UserInfoSettingViewController alloc] init];
        [KEY_WINDOW addTransitionAnimationWithDuration:0.5f type:TransitionOglFlip subType:FROM_LEFT];
        [self.navigationController pushViewController:setting animated:YES];
    };
    
    self.headerView.headerSubview.settingBlock = ^{
        @strongify(self);
        CLSettingViewController *setting = [[CLSettingViewController alloc] init];
        [self.navigationController pushViewController:setting animated:YES];
    };
}

- (HFStretchableTableHeaderView *)stretchHeaderView
{
    if (!_stretchHeaderView)
    {
        _stretchHeaderView = [[HFStretchableTableHeaderView alloc] init];
    }
    return _stretchHeaderView;
}

- (void)settingAction {

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.stretchHeaderView scrollViewDidScroll:scrollView];
}

- (void)viewDidLayoutSubviews
{
    [self.stretchHeaderView resizeView];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMeTableViewCellId];
    
    if (indexPath.row == 0 || indexPath.row == 5) {
        cell.arrowImgView.hidden = YES;
    }
    
    if (indexPath.row != 0) {
        cell.mobileLabel.hidden = YES;
    }
    
    cell.titleLabel.text = _titles[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
        {
            NSString *mobileNum = [@"telprompt://" stringByAppendingString:@"0531-58051562"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mobileNum]];
        }
            break;
        case 1:
        {
            [self pushToNextPageWithClass:[ModifyPsdViewController class]];
            
        }
            break;
        case 2:
        {
            [self pushToNextPageWithClass:[AboutUsViewController class]];
        }
            break;
        case 3:
        {
            [self pushToNextPageWithClass:[Feedback1ViewController class]];
        }
            break;
        case 4:
        {
            TabBarController *tab = [[TabBarController alloc] init];
            [KEY_WINDOW addTransitionAnimationWithDuration:0.5f type:TransitionCube subType:FROM_BOTTOM];
            KEY_WINDOW.rootViewController = tab;
        }
            break;
        default:
            break;
    }
}

- (void)pushToNextPageWithClass:(Class)class {
    [self.navigationController pushViewController:[self getViewControllerFromClass:class] animated:YES];
}

- (UIViewController *)getViewControllerFromClass:(Class)class {
    return [[class alloc]init];
}

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@"联系我们",@"修改密码",@"关于我们",@"意见反馈",@"退出平台"];
    }
    return _titles;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
